﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Web.UI;
using System.Web.UI.WebControls;
using Alogient.Cameleon.Sdk.Items;

namespace Alogient.Cameleon.Sdk.UI.WebControls
{
    [ToolboxData("<{0}:CMRepeater runat=server></{0}:CMRepeater>")]
    public class CMRepeater : Repeater
    {
        protected int navID = NavigationController.GetNavID();
        protected string cultureCode = NavigationController.GetCultureCode();
        
        [Category("Cameleon")]
        [Browsable(true)]
        public int NavID
        {
            get
            {
                return this.navID;
            }

            set
            {
                this.navID = value;
            }
        }

        [Category("Cameleon")]
        [Browsable(true)]
        public string CultureCode
        {
            get
            {
                return this.cultureCode;
            }

            set
            {
                this.cultureCode = value;
            }
        }

        [Category("Cameleon")]
        [Browsable(true)]
        public string TemplateFilter { get; set; }

        protected override void OnLoad(EventArgs e)
        {
            if (this.DataSource == null)
            {
                if (!string.IsNullOrEmpty(this.TemplateFilter))
                {
                    var tempSubpages = new CMPage(this.navID, this.cultureCode).Subpages;
                    var subpages = new CMPageCollection();
                    
                    subpages.AddRange(tempSubpages.Where(page => page.TemplateName == this.TemplateFilter));

                    this.DataSource = subpages;
                }
                else
                {
                    this.DataSource = new CMPage(this.navID, this.cultureCode).Subpages;
                }

                this.DataBind();
            }

            base.OnLoad(e);
        }
    }
}
